package com.ruoyi.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.mapper.GroundMapper;
import com.ruoyi.service.GroundService;
import com.ruoyi.utils.ElongUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class GroundServiceImpl implements GroundService {

    private static Logger hotelSuccess = LoggerFactory.getLogger("hotel_success");

    @Value("${eLong.reqUrl}")
    private String reqUrl;

    @Autowired
    GroundMapper groundMapper;

    @Override
    public String getLoginToken() {
        Map map = groundMapper.getLoginToken();
        if (map == null) {
            // 生成accessToken
            String overdueTimeStr = DateUtils.getOverdueTime(1);
            String loginToken = ElongUtils.login();
            groundMapper.insertLoginToken(loginToken, overdueTimeStr);
            return loginToken;
        } else {
            String overdueTimeStr = map.get("overdue_time").toString();
            String loginToken = map.get("login_token").toString();
            LocalDateTime now = LocalDateTime.now();
            LocalDateTime overdueTimeDate = LocalDateTime.parse(overdueTimeStr);
            if (now.isBefore(overdueTimeDate)) {
                return loginToken;
            } else {
                overdueTimeStr = DateUtils.getOverdueTime(1);
                loginToken = ElongUtils.login();
                groundMapper.updateLoginToken(loginToken, overdueTimeStr);
                return loginToken;
            }
        }
    }

    @Override
    public JSONObject pushAutoMapping(String supplierHotelId, String supplierHotelName, String supplierRoomTypeId, String supplierRoomTypeName, String supplierRatePlanId, String supplierRatePlanName, String mHotelId, String mHotelName, String mRoomId, String mRoomName, String misSupplierId) {
        hotelSuccess.info("自动化上线接口调用:");
        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        jsonObject1.put("supplierHotelId", supplierHotelId);
        jsonObject1.put("supplierHotelName", supplierHotelName);
        jsonObject1.put("supplierRoomTypeId", supplierRoomTypeId);
        jsonObject1.put("supplierRoomTypeName", supplierRoomTypeName);
        jsonObject1.put("supplierRatePlanId", supplierRatePlanId);
        jsonObject1.put("supplierRatePlanName", supplierRatePlanName);
        jsonObject1.put("mHotelId", mHotelId);
        jsonObject1.put("mHotelName", mHotelName);
        jsonObject1.put("mRoomId", mRoomId);
        jsonObject1.put("mRoomName", mRoomName);
        jsonObject1.put("misSupplierId", misSupplierId);
        jsonArray.add(jsonObject1);
        jsonObject.put("autoMappingReqList", jsonArray);
        JSONObject sendJsonObj = getShareJSONObject("pushAutoMapping", jsonObject);
        String sendJsonStr = JSONObject.toJSONString(sendJsonObj);
        hotelSuccess.info("自动化上线接口请求报文:" + sendJsonStr);
        String res = HttpUtils.sendPostJson(reqUrl, sendJsonStr);
        hotelSuccess.info("自动化上线接口响应报文:" + res);
        JSONObject resJonObj = JSONObject.parseObject(res);
        return resJonObj;
    }

    @Override
    public JSONObject offlineMapping(String offlineType, String hotelId, String houseId, String rpId) {
        hotelSuccess.info("自动化下线接口调用:");
        JSONObject jsonObject = new JSONObject();
        JSONArray roomTypeIdList = new JSONArray();
        roomTypeIdList.add(houseId);
        JSONArray ratePlanIdList = new JSONArray();
        ratePlanIdList.add(rpId);
        jsonObject.put("offlineType", offlineType);
        if ("Hotel".equals(offlineType)) {
            jsonObject.put("hotelId", hotelId);
        } else if ("RoomType".equals(offlineType)) {
            jsonObject.put("hotelId", hotelId);
            jsonObject.put("roomTypeIdList", roomTypeIdList);
        } else if ("RatePlan".equals(offlineType)) {
            jsonObject.put("hotelId", hotelId);
            jsonObject.put("roomTypeIdList", roomTypeIdList);
            jsonObject.put("ratePlanIdList", ratePlanIdList);
        }
        JSONObject sendJsonObj = getShareJSONObject("offlineMapping", jsonObject);
        String sendJsonStr = JSONObject.toJSONString(sendJsonObj);
        hotelSuccess.info("自动化下线接口请求报文:" + sendJsonStr);
        String res = HttpUtils.sendPostJson(reqUrl, sendJsonStr);
        hotelSuccess.info("自动化下线接口响应报文:" + res);
        JSONObject resJonObj = JSONObject.parseObject(res);
        return resJonObj;
    }

    @Override
    public JSONObject getMapping(String hotelId) {
        hotelSuccess.info("获取产品创建结果调用:");
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(hotelId);
        jsonObject.put("hotelIds", jsonArray);
        JSONObject sendJsonObj = getShareJSONObject("getMapping", jsonObject);
        String sendJsonStr = JSONObject.toJSONString(sendJsonObj);
        hotelSuccess.info("获取产品创建结果请求报文:" + sendJsonStr);
        String res = HttpUtils.sendPostJson(reqUrl, sendJsonStr);
        hotelSuccess.info("获取产品创建结果响应报文:" + res);
        JSONObject resJonObj = JSONObject.parseObject(res);
        return resJonObj;
    }

    @Override
    public JSONObject updateRoomRatePlanPolicy(String hotelCode, String rpId, String beginDate, boolean canBeCanceled, int cancelBeforeDays, int cancelBeforeTime, int hour, int cashScaleType, String endDate, int percentage) {
        hotelSuccess.info("取消规则推送调用:");
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("beginDate", beginDate);
        jsonObject1.put("canBeCanceled", canBeCanceled);
        jsonObject1.put("cancelBeforeDays", cancelBeforeDays);
        jsonObject1.put("cancelBeforeTime", cancelBeforeTime);
        jsonObject1.put("hour", hour);
        jsonObject1.put("cashScaleType", cashScaleType);
        jsonObject1.put("endDate", endDate);
        jsonObject1.put("percentage", percentage);
        jsonArray.add(jsonObject1);
        jsonObject.put("hotelCode", hotelCode);
        jsonObject.put("ratePlanCode", rpId);
        jsonObject.put("prepayPolicy", jsonArray);
        JSONObject sendJsonObj = getShareJSONObject("updateRoomRatePlanPolicy", jsonObject);
        String sendJsonStr = JSONObject.toJSONString(sendJsonObj);
        hotelSuccess.info("取消规则请求报文:" + sendJsonStr);
        String res = HttpUtils.sendPostJson(reqUrl, sendJsonStr);
        hotelSuccess.info("取消规则响应报文:" + res);
        JSONObject resJonObj = JSONObject.parseObject(res);
        return resJonObj;
    }

    @Override
    public JSONObject updateRoomTypeStatus(String hotelCode, String beginDate, String endDate, int inventoryUpdateType, List<String> roomTypeCode, String status) {
        hotelSuccess.info("房态调用:");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("hotelCode", hotelCode);

        JSONArray jsonArray = new JSONArray();
        for (String houseId : roomTypeCode) {
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("beginDate", beginDate);
            jsonObject1.put("endDate", endDate);
            jsonObject1.put("inventoryUpdateType", inventoryUpdateType);
            jsonObject1.put("roomTypeCode", houseId);
            jsonObject1.put("status", status);
            jsonArray.add(jsonObject1);
        }
        jsonObject.put("roomTypeList", jsonArray);
        JSONObject sendJsonObj = getShareJSONObject("updateRoomTypeStatus", jsonObject);
        String sendJsonStr = JSONObject.toJSONString(sendJsonObj);
        hotelSuccess.info("房态调用请求报文:" + sendJsonStr);
        String res = HttpUtils.sendPostJson(reqUrl, sendJsonStr);
        hotelSuccess.info("房态调用响应报文:" + res);
        JSONObject resJonObj = JSONObject.parseObject(res);
        return resJonObj;
    }

    @Override
    public JSONObject updateRoomTypeAmount(String hotelCode, int amount, String arriveDate, String leaveDate, int inventoryUpdateType, List<String> roomTypeCode, String status) {
        hotelSuccess.info("房量调用:");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("hotelCode", hotelCode);
        JSONArray jsonArray = new JSONArray();
        for (String houseId : roomTypeCode) {
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("amount", amount);
            jsonObject1.put("arriveDate", arriveDate);
            jsonObject1.put("leaveDate", leaveDate);
            jsonObject1.put("inventoryUpdateType", inventoryUpdateType);
            jsonObject1.put("roomTypeCode", houseId);
            jsonObject1.put("status", status);
            jsonArray.add(jsonObject1);
        }
        jsonObject.put("roomTypeList", jsonArray);
        JSONObject sendJsonObj = getShareJSONObject("updateRoomTypeAmount", jsonObject);
        String sendJsonStr = JSONObject.toJSONString(sendJsonObj);
        hotelSuccess.info("房量调用请求报文:" + sendJsonStr);
        String res = HttpUtils.sendPostJson(reqUrl, sendJsonStr);
        hotelSuccess.info("房量调用响应报文:" + res);
        JSONObject resJonObj = JSONObject.parseObject(res);
        return resJonObj;
    }

    @Override
    public JSONObject updatePriceInfoOpen(String hotelCode, int addBedPrice, String currencyCode, String priceType, String rateCode, String roomTypeCode, List<Map<String, Object>> lists) {
        hotelSuccess.info("有效价格调用:");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("hotelCode", hotelCode);
        jsonObject.put("invalidPriceList", new JSONArray());
        JSONArray jsonArray = new JSONArray();
        for (Map map : lists) {
            List<String> days = (List) map.get("days");
            if (days.size() > 0) {
                String ePrice = map.get("ePrice").toString();
                String beginDate = days.get(0);
                String endDate = days.get(days.size() - 1);
                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.put("addBedPrice", addBedPrice);
                jsonObject1.put("beginDate", beginDate);
                jsonObject1.put("endDate", endDate);
                jsonObject1.put("currencyCode", currencyCode);
                jsonObject1.put("priceType", priceType);
                jsonObject1.put("price", ePrice);
                jsonObject1.put("rateCode", rateCode);
                jsonObject1.put("roomTypeCode", roomTypeCode);
                jsonArray.add(jsonObject1);
            }
        }
        jsonObject.put("priceItemList", jsonArray);
        JSONObject sendJsonObj = getShareJSONObject("updatePriceInfo", jsonObject);
        String sendJsonStr = JSONObject.toJSONString(sendJsonObj);
        hotelSuccess.info("有效价格请求报文:" + sendJsonStr);
        String res = HttpUtils.sendPostJson(reqUrl, sendJsonStr);
        hotelSuccess.info("有效价格响应报文:" + res);
        JSONObject resJonObj = JSONObject.parseObject(res);
        return resJonObj;
    }

    @Override
    public JSONObject updatePriceInfoClose(String hotelCode, String beginDate, String endDate, String rateCode, String roomTypeCode) {
        hotelSuccess.info("无效价格调用:");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("hotelCode", hotelCode);
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("beginDate", beginDate);
        jsonObject1.put("endDate", endDate);
        jsonObject1.put("rateCode", rateCode);
        jsonObject1.put("roomTypeCode", roomTypeCode);
        jsonArray.add(jsonObject1);
        jsonObject.put("invalidPriceList", jsonArray);
        jsonObject.put("priceItemList", new JSONArray());
        JSONObject sendJsonObj = getShareJSONObject("updatePriceInfo", jsonObject);
        String sendJsonStr = JSONObject.toJSONString(sendJsonObj);
        hotelSuccess.info("无效价格请求报文:" + sendJsonStr);
        String res = HttpUtils.sendPostJson(reqUrl, sendJsonStr);
        hotelSuccess.info("无效价格响应报文:" + res);
        JSONObject resJonObj = JSONObject.parseObject(res);
        return resJonObj;
    }

    @Override
    public JSONObject processFreeCancelPolicy(String hotelId, int opType) {
        hotelSuccess.info("30分钟免费取消调用:");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("minutes", 30);
        jsonObject.put("op_type", opType);
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(hotelId);
        jsonObject.put("hotelIds", jsonArray);
        JSONObject sendJsonObj = getShareJSONObject("processFreeCancelPolicy", jsonObject);
        String sendJsonStr = JSONObject.toJSONString(sendJsonObj);
        hotelSuccess.info("30分钟免费取消请求报文:" + sendJsonStr);
        String res = HttpUtils.sendPostJson(reqUrl, sendJsonStr);
        hotelSuccess.info("30分钟免费取消响应报文:" + res);
        JSONObject resJonObj = JSONObject.parseObject(res);
        return resJonObj;
    }

    /**
     * 返回一个用法请求同程的JsonObject
     *
     * @param method 方法名
     * @return
     */
    public JSONObject getShareJSONObject(String method, JSONObject body) {
        String loginToken = getLoginToken();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("accessToken", loginToken);
        jsonObject.put("apiKey", "testapikey");
        jsonObject.put("body", body);
        jsonObject.put("encrypted", 0);
        jsonObject.put("guid", UUID.randomUUID().toString());
        jsonObject.put("method", method);
        jsonObject.put("timestamp", System.currentTimeMillis());
        jsonObject.put("version", "v2");
        return jsonObject;
    }
}
